import type { LabeledValue } from 'ant-design-vue/es/select';

type RowOptionsParams<T> = {
  labelKey: string;
  valueKey: string;
  iconKey?: string;
  raw?: T;
  rows: T[];
};

/* 把数据库的值转成下拉列表,菜单等 */
export const rowsToOptions = <T>(config: RowOptionsParams<T>): LabeledValue[] => {
  return config.rows?.map(e => {
    return {
      label: (e as any)[config.labelKey],
      value: (e as any)[config.valueKey],
      title: (e as any)[config.labelKey],
      key: (e as any)[config.labelKey],
      raw: e,
    };
  });
};
export const isDev = () => {
  return import.meta.env.MODE === 'development';
};
export const fetchOptions = <T>(options: Ref<LabeledValue[]>, data: RowOptionsParams<T>) => {
  if (options.value.length === 0) {
    if (!data.rows) {
      data.rows = [];
    }
    options.value = rowsToOptions(data);
  }
  return options.value;
};
